const koa = require("koa");
const router = require("koa-router")();
const Top250Model = require("./models/top250");
const cors = require("koa2-cors");
const bodyParser = require("koa-bodyparser");
const app = new koa();
//获取数据库列表数据
router.get("/top250",async ctx=>{
    var data = await Top250Model.find({})
    ctx.body = data
}) 
//提交表单
router.post("/add",async ctx=>{
    // console.log(ctx.request.body);
    var {name,rating} = ctx.request.body;//ctx.request.body是提交的数据
    var db = new Top250Model({name,rating});
    db.save(err=>{
        if(err) throw err;
    })
    ctx.redirect("/top250")
})
// 删除
router.post("/doDelete",async ctx=>{
    // console.log(ctx.request.body);
    var {id} = ctx.request.body;
    await Top250Model.remove({_id:id});
    ctx.redirect("/top250")
})
app.use(cors());
app.use(bodyParser());
app.use(router.routes());
app.listen(8080);